package com.upcoming.data.storm.util;

import com.google.gson.Gson;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Event implements Serializable {

	private static final long serialVersionUID = -741669906048474561L;
	private String eventCode = null;//事件编码
	private String eventMsg = null;//消息描述
	private String objectId = null;//
	private long id = 0L;//事件的唯一标识
	private long time = 0l;//事件的发送时间
	private boolean isUnicast = false;//是否单一传播
	private HashMap<String, Serializable> eventProperty = null;


	public Event(String eventCode, String eventMsg, HashMap<String, Serializable> eventProperty) {
		this.eventCode = eventCode;
		this.eventMsg = eventMsg;
		this.eventProperty = eventProperty;
	}

	public Event(String eventCode, String eventMsg, String objectId) {
		this.eventCode = eventCode;
		this.eventMsg = eventMsg;
		this.objectId = objectId;
	}

	public Event(String eventCode) {
		this.eventCode = eventCode;
	}

	public Event() {

	}


	/**
	 * 添加一个属性
	 *
	 * @param key
	 * @param val
	 */
	public void addProperty(String key, Serializable val) {
		if (this.eventProperty == null) {
			this.eventProperty = new HashMap<String, Serializable>();
		}
		this.eventProperty.put(key, val);
	}

	/**
	 * 使用addProperty() 替代
	 *
	 * @param map
	 */
	@Deprecated
	public void addProperty(Map<String, Serializable> map) {
		if (this.eventProperty == null) {
			this.eventProperty = new HashMap<String, Serializable>();
		}
		this.eventProperty.putAll(map);
	}

	/**
	 * 删除一个属性
	 *
	 * @param key
	 */
	public void removeProperty(String key) {
		if (this.eventProperty == null) {
			return;
		}
		this.eventProperty.remove(key);
	}

	/**
	 * 是否包含某个属性
	 *
	 * @param key
	 * @return
	 */
	public boolean containsProperty(String key) {
		if (this.eventProperty == null) {
			return false;
		}
		return this.eventProperty.containsKey(key);
	}

	/**
	 * 获取属性值
	 *
	 * @param key
	 * @return
	 */
	public Serializable getProperty(String key) {
		if (this.eventProperty == null) {
			return null;
		}
		return this.eventProperty.get(key);
	}

	/**
	 * 清除属性
	 */
	public void clearProperty() {
		if (this.eventProperty == null) {
			return;
		}
		this.eventProperty.clear();
	}

	@Override
	public String toString() {
		return "Event{" +
				"eventCode='" + eventCode + '\'' +
				", eventMsg='" + eventMsg + '\'' +
				", objectId='" + objectId + '\'' +
				", eventProperty=" + eventProperty +
				", id=" + id +
				", time=" + time +
				'}';
	}

	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	/**
	 * 将Event对象转化为Json 字符串
	 *
	 * @return Json 字符串
	 */
	public String toJson() {
		Gson gson = new Gson();
		return gson.toJson(this);
	}

	public String getEventCode() {
		return eventCode;
	}

	public void setEventCode(String eventCode) {
		this.eventCode = eventCode;
	}

	public String getEventMsg() {
		return eventMsg;
	}

	public void setEventMsg(String eventMsg) {
		this.eventMsg = eventMsg;
	}

	public String getObjectId() {
		return objectId;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public HashMap<String, Serializable> getEventProperty() {
		return eventProperty;
	}

	public void setEventProperty(HashMap<String, Serializable> eventProperty) {
		this.eventProperty = eventProperty;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public boolean isUnicast() {
		return isUnicast;
	}

	public void setUnicast(boolean isUnicast) {
		this.isUnicast = isUnicast;
	}
}
